package app;

import java.util.*;

import utils.FilePack;

import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

import com.StarMicronics.StarIOSDK.R;

public class AddWaterView {
	
	private long ticketID;

	private void back() {
		AppDelegate.sharedInstance().layout.removeView(view);
	}
	
	private void save() {
		Dictionary<String, Object> dic = AppDelegate.sharedInstance().getMain().ticketsDataSource.memoryObject.get(Long.valueOf(ticketID));
		if (dic != null) {
			dic.put(TicketList.DRIVER_NOTE, note.getText().toString());
			dic.put(TicketList.ADD_WATER_GAL, gal.getText().toString());
			
			Vector<Long> changed = new Vector<Long>();
			changed.add(Long.valueOf(ticketID));
			Dictionary<String, Object> temp = new Hashtable<String, Object>();
			temp.put("changed", changed);
			AppDelegate.sharedInstance().postNotification(DataSourceChangeNotificationListener.class, AppDelegate.sharedInstance().getMain().ticketsDataSource, temp);
			back();
		}
	}
	
	public AddWaterView(long params) {
		loadXmlView();
		
		ticketID = params;
		Dictionary<String, Object> dic = AppDelegate.sharedInstance().getMain().ticketsDataSource.memoryObject.get(Long.valueOf(ticketID));
		if (dic != null) {
			note.setText((String)dic.get(TicketList.DRIVER_NOTE));
			gal.setText((String)dic.get(TicketList.ADD_WATER_GAL));
		}
	}
	
	private View view;
	private TextView label1;
	private TextView label2;
	private EditText gal;
	private Button button_back;
	private Button button_save;
	private EditText note;
	private void loadXmlView() {
        view = AppDelegate.sharedInstance().activity.getLayoutInflater().inflate(R.layout.add_water, null);
        label1 = (TextView)view.findViewById(R.id.label1);
        label2 = (TextView)view.findViewById(R.id.label2);
        gal = (EditText)view.findViewById(R.id.gal);
        button_back = (Button)view.findViewById(R.id.button_back);
        button_save = (Button)view.findViewById(R.id.button_save);
        note = (EditText)view.findViewById(R.id.note);
        
        label1.setText(FilePack.getStringByID(STRING_ID.STR_ADD_WATER_HINT));
        label2.setText(FilePack.getStringByID(STRING_ID.STR_DRIVER_NOTE));
        gal.setHint(FilePack.getStringByID(STRING_ID.STR_GAL));
        button_back.setText(FilePack.getStringByID(STRING_ID.STR_BACK));
        button_save.setText(FilePack.getStringByID(STRING_ID.STR_SAVE));
        note.setHint(FilePack.getStringByID(STRING_ID.STR_NOTE));
        
        button_back.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				back();
			}
		});
        button_save.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				save();
			}
		});
        view.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				back();
			}
		});
        
		AppDelegate.sharedInstance().layout.addView(view);
	}
}
